package com.jy.api.controller.web;

import com.jy.api.service.CouponInstanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * Created by lihu on 2017/11/20.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/web", produces = "application/json;charset=utf-8")
public class WebCouponInstanceController {

    @Autowired
    CouponInstanceService couponInstanceService;

    /**
     * 获取会员对应店铺优惠卷
     *
     * @param memberId
     * @param storeId
     * @param pageable
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{memberId}/coupon-instance")
    public ResponseEntity queryStoreMemberCoupon(@PathVariable String memberId,
                                                 @RequestParam(name = "storeId") String storeId,
                                                 @PageableDefault(value = 10, sort = {"endTime"}, direction = Sort.Direction.ASC) Pageable pageable) {
        try {
            return new ResponseEntity(couponInstanceService.getMemberStoreCouponInstance(memberId, storeId, pageable), HttpStatus.OK);
        } catch (Exception ex) {
            log.error("queryStoreMemberCoupon is error==> memberId:{},storeId:{}", memberId, storeId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
